package service;

import dao.*;
import domin.*;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;


public final class OrdinaryUserService {
    private static OrdinaryUserDao ordinaryUserDao
            = OrdinaryUserDao.getInstance();

    private static OrdinaryUserService ordinaryUserService
            =new OrdinaryUserService();
    private OrdinaryUserService(){}
    public static OrdinaryUserService getInstance(){
        return ordinaryUserService;
    }
    public Collection<OrdinaryUser> findAll(Collection<Condition> condition, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<OrdinaryUser> ordinaryUsers = ordinaryUserDao.findAll(connection,condition,pagination);
        JdbcHelper.close(connection);
        return ordinaryUsers;
    }
    public OrdinaryUser find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        OrdinaryUser ordinaryUser = ordinaryUserDao.find(connection,id);
        JdbcHelper.close(connection);
        return ordinaryUser;
    }
    public Collection<OrdinaryUser> getAllWithCondition(String s) throws SQLException{
        Collection<OrdinaryUser>  collection = new HashSet<OrdinaryUser>();
        Connection connection = JdbcHelper.getConn();
        Collection<OrdinaryUser> ordinaryUsers = OrdinaryUserDao.getInstance().getAllWithCondition(connection,s);
        collection.addAll(ordinaryUsers);
        JdbcHelper.close(connection);
        return collection;
    }

    public boolean update(OrdinaryUser ordinaryUser) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = ordinaryUserDao.update(connection,ordinaryUser);
        JdbcHelper.close(connection);
        return updated;
    }
    public boolean add(OrdinaryUser ordinaryUser) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean added = false;
        try{
            String name = ordinaryUser.getName();
            Actor actor = new Actor(name);
            int actor_id=ActorDao.getInstance().add(connection,actor);
            Actor finalActor = new Actor(name,actor_id);
            String password = "123456";
            Users users = new Users(name,password,finalActor);
            UsersDao.getInstance().add(connection,users);
            Role role = RoleDao.getInstance().find(connection,2);
            ActorAssRole actorAssRole = new ActorAssRole(finalActor,role);
            ActorAssRoleDao.getInstance().add(connection,actorAssRole);
            added =  ordinaryUserDao.getInstance().add(connection,ordinaryUser);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }
    public boolean delete(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = ordinaryUserDao.delete(connection,id);
        JdbcHelper.close(connection);
        return deleted;
    }
}

